package org.example.servletpractice.mapper;


import java.sql.*;

public class JDBC {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";
    static final String USER = "root";
    static final String PASSWORD = "root";

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection(){
        Connection connection =null;
        try {
            connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
        }catch (Exception s){
            System.out.println("获取连接失败: "+s);
        }
        return connection;
    }

    public static void close(Connection connection, Statement statement, PreparedStatement preparedStatement,ResultSet resultSet ) throws SQLException {
        if(connection!=null){
            connection.close();
        }
        if(statement!=null){
            statement.close();
        }
        if(preparedStatement!=null){
            preparedStatement.close();
        }
        if(resultSet!=null){
            resultSet.close();
        }
    }

}
